package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.PmsProduct;
import com.itheima.domain.SmsFlashPromotionProductRelation;
import com.itheima.mapper.PmsProductMapper;
import com.itheima.mapper.SmsFlashPromotionProductRelationMapper;
import com.itheima.service.db.SmsFlashPromotionProductRelationService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ChenTianSui
 * @date 2022/11/13 0:05
 */
@DubboService
public class SmsFlashPromotionProductRelationServiceImpl implements SmsFlashPromotionProductRelationService {
    @Autowired
    private SmsFlashPromotionProductRelationMapper productRelationMapper;
    @Autowired
    private PmsProductMapper pmsProductMapper;

    @Override
    public PageBeanVo findListByPage(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer papageSize) {
        //构造分页条件
        Page<SmsFlashPromotionProductRelation> pageInfo = new Page<>(pageNum, papageSize);
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> qw = new LambdaQueryWrapper<>();
        qw.eq(SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        qw.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId);
        qw.orderByDesc(SmsFlashPromotionProductRelation::getSort);
        productRelationMapper.selectPage(pageInfo, qw);
        //获取分页里的集合数据
        List<SmsFlashPromotionProductRelation> list = pageInfo.getRecords();
        List<Map<String, Object>> pageList = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            pageList = list.stream().map(
                    e -> {
                        Map<String, Object> map = new HashMap<>();
                        PmsProduct pmsProduct = pmsProductMapper.selectById(e.getProductId());
                        map.put("id", e.getId());
                        map.put("flashPromotionId", e.getFlashPromotionId());
                        map.put("flashPromotionSessionId", e.getFlashPromotionSessionId());
                        map.put("productId", e.getProductId());
                        map.put("flashPromotionPrice", e.getFlashPromotionPrice());
                        map.put("flashPromotionCount", e.getFlashPromotionCount());
                        map.put("flashPromotionLimit", e.getFlashPromotionLimit());
                        map.put("sort", e.getSort());
                        map.put("product", pmsProduct);
                        return map;
                    }
            ).collect(Collectors.toList());

        }
        return new PageBeanVo(pageNum, papageSize, pageInfo.getTotal(), pageList);
    }

    @Override
    public void create(List<SmsFlashPromotionProductRelation> relationList) {
        if (CollUtil.isNotEmpty(relationList)) {
            for (SmsFlashPromotionProductRelation productRelation : relationList) {
                productRelationMapper.insert(productRelation);
            }
        }

    }

    @Override
    public void delete(Integer id) {
        productRelationMapper.deleteById(id);

    }

    @Override
    public void update(Integer id, SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        LambdaQueryWrapper<PmsProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(PmsProduct::getId, smsFlashPromotionProductRelation.getProductId());
        //获取对应商品信息
        PmsProduct pmsProduct = pmsProductMapper.selectOne(qw);
        BigDecimal price = pmsProduct.getPrice();//商品价格
        Integer stock = pmsProduct.getStock();//库存数量
        BigDecimal flashPromotionPrice = smsFlashPromotionProductRelation.getFlashPromotionPrice();//秒杀价格
        Integer flashPromotionCount = smsFlashPromotionProductRelation.getFlashPromotionCount();//秒杀数量
        smsFlashPromotionProductRelation.getFlashPromotionLimit();//每人限购数量

        smsFlashPromotionProductRelation.setId(id.longValue());
        productRelationMapper.updateById(smsFlashPromotionProductRelation);


    }

    @Override
    public SmsFlashPromotionProductRelation findById(Integer id) {
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation = productRelationMapper.selectById(id);
        return smsFlashPromotionProductRelation;

    }
}
 
